package com.bayss.core.service.user;


import com.bayss.core.entity.rbac.OBGUserInfo;
import com.bayss.core.entity.rbac.OBGUserInfoExample;
import com.bayss.core.entity.rbac.mapper.OBGUserInfoMapper;
import com.bayss.core.entity.rbac.query.AdminQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ObgUserService {

    private static Logger logger = LogManager.getLogger();

    @Autowired
    private OBGUserInfoMapper obgUserInfoMapper;


    public OBGUserInfo getUser(Integer uid) {
        return obgUserInfoMapper.selectByPrimaryKey(uid);
    }

    public List<OBGUserInfo> findByIdList(List<Integer> list) {
        OBGUserInfoExample example = new OBGUserInfoExample();
        example.createCriteria().andIdIn(list);
        return obgUserInfoMapper.selectByExample(example);
    }

    public OBGUserInfo findByAccount(String account, String password) {
        OBGUserInfoExample example = new OBGUserInfoExample();
        OBGUserInfoExample.Criteria criteria = example.createCriteria();
        criteria.andAccountEqualTo(account);
        criteria.andPasswordEqualTo(password);
        List<OBGUserInfo> obgUserInfoList = obgUserInfoMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(obgUserInfoList)) {
            return obgUserInfoList.get(0);
        }
        return null;
    }

    public PageInfo<OBGUserInfo> findPage(AdminQuery adminQuery) {
        OBGUserInfoExample example = new OBGUserInfoExample();
        OBGUserInfoExample.Criteria criteria = example.createCriteria();
        // 手机号匹配
        if (!StringUtils.isEmpty(adminQuery.getCellphone())) {
            criteria.andCellphoneEqualTo(adminQuery.getCellphone());
        }
        // 邮箱匹配
        if (!StringUtils.isEmpty(adminQuery.getEmail())) {
            criteria.andEmailEqualTo(adminQuery.getEmail());
        }
        // 名称匹配
        if (!StringUtils.isEmpty(adminQuery.getNickname())) {
            criteria.andNicknameEqualTo(adminQuery.getNickname());
        }
        // 开始时间 和 结束时间
        if (!StringUtils.isEmpty(adminQuery.getStartTime())) {
            criteria.andCreateTimeBetween(adminQuery.getStartTime(), adminQuery.getEndTime());
        }
        // 状态匹配
        if (!StringUtils.isEmpty(adminQuery.getStatus())) {
            criteria.andStatusEqualTo(adminQuery.getStatus());
        }
        PageInfo<OBGUserInfo> result = PageHelper.startPage(adminQuery.getPage(), adminQuery.getSize()).doSelectPageInfo(() -> obgUserInfoMapper.selectByExample(example));
        return result;
    }
}
